package com.icinfo.cloud.provider.punish.common.documentdataload.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.enums.DocumentKindEnum;
import com.icinfo.cloud.provider.punish.common.documentdataload.dto.CaseDataQueryDto;
import com.icinfo.cloud.provider.punish.common.documentdataload.dto.CaseDocumentDto;
import com.icinfo.cloud.provider.punish.common.documentdataload.service.IDocumentDataLoadService;
import com.icinfo.cloud.provider.punish.common.documentdataload.vo.CaseDocumentVo;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiParamDocumentInfoService;
import com.icinfo.cloud.provider.punish.evideinv.dto.QueryEvidencePageDto;
import com.icinfo.cloud.provider.punish.evideinv.service.EvidenceInventoryService;
import com.icinfo.cloud.provider.punish.evideinv.vo.EvidencePageNewVo;
import com.icinfo.cloud.provider.punish.fastcase.vo.FastCaseDocumentDataVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseStageEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseDocumentRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseOfflineDocumentRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseOfflineDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseDocumentRecordCodeNameVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseDocumentRecordVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc 文书数据获取service接口实现类
 * @Author mahan
 * @Date 2023/2/22
 */
@Service
@Slf4j
public class DocumentDataLoadServiceImpl implements IDocumentDataLoadService {

	@Resource
	private IConfigBusiParamDocumentInfoService configBusiParamDocumentInfoService;
	@Resource
	private ICaseMainInfoService caseMainInfoService;
	@Resource
	private ICaseDocumentRecordService documentRecordService;
	@Resource
	private EvidenceInventoryService evidenceInventoryService;
	@Resource
	private ICaseOfflineDocumentRecordService caseOfflineDocumentRecordService;
	/**
	 * 获取补正/更正文书中特定的信息
	 * @return
	 */
	public CaseDocumentVo getDocumentInfo(CaseDocumentDto dto){
		CaseDocumentVo reVO = new CaseDocumentVo();
		//获取主表信息
		CaseMainInfoVo caseMainInfoVo = caseMainInfoService.getCaseMainInfo(dto.getCaseId());
		//获取文号和送达时间
		CaseDocumentRecordVo recordVo = documentRecordService.getCaseDocumentRecordInfo(dto.getBeServedDocumentId());
		reVO.setDocumentNumber(recordVo.getDocumentNumber());
		if(recordVo.getSentTime() != null){
			reVO.setSentTime(recordVo.getSentTime());
		}
		//获取文书名称
		ConfigBusiParamDocumentInfo documentInfo = configBusiParamDocumentInfoService.getConfigBusiParamDocumentInfoByBusiParamIdAndCode(caseMainInfoVo.getBusiParamId(), recordVo.getDocumentCatalogCode());
		reVO.setDocumentName(documentInfo.getDocumentCatalogName());

		return reVO;
	}

	/**
	 * 【V1.11.3】描述： 获取陈述申辩相关内容
	 *
	 * @author: zhengqiang
	 * @date: 2023/4/7
	 * @param: dto
	 * @return {@link Map< String, Object>}
	 **/
	@Override
	public Map<String,Object> getStatementDefendContent(CaseDataQueryDto dto) {
		Map<String,Object> result = new HashMap<>();
		String caseId = dto.getCaseId();
		//获取主表信息
		CaseMainInfoVo caseMainInfoVo = caseMainInfoService.getCaseMainInfo(caseId);
		String partyId = dto.getPartyId();
		//多当事人合并处罚不传partyId
		if (ObjectUtil.isNotEmpty(partyId) && partyId.contains(",")) {
			partyId = null;
		}
		String kindCode = DocumentKindEnum.DOC_KIND_DK2CSSBBL.getCode();
		ConfigBusiParamDocumentInfo documentInfo = configBusiParamDocumentInfoService.getDocumentInfoByDocumentKindCode(caseMainInfoVo.getBusiParamId(), kindCode);

		CaseDocumentRecord caseDocumentRecord = documentRecordService.getCaseDocumentRecord(caseId, documentInfo.getDocumentCatalogCode(), partyId);
		if (ObjectUtil.isNotEmpty(caseDocumentRecord)) {
			result = JSONObject.parseObject(caseDocumentRecord.getDocumentContent(), Map.class);
			result.remove("mainHandleSignPic");
			result.remove("assistHandleSignPic");
		}
		return result;
	}

	@Override
	public FastCaseDocumentDataVo getEvidenceNames(String caseId){
		CaseMainInfoVo caseMainInfoVo = caseMainInfoService.getCaseMainInfo(caseId);

		List<String> reList = CollUtil.newArrayList();
		//获取线下证据
		QueryEvidencePageDto evidenceQueryDto = new QueryEvidencePageDto();
		evidenceQueryDto.setCaseId(caseId);
		evidenceQueryDto.setPageSize(1000);
		evidenceQueryDto.setPageNum(1);
		PageResponse<EvidencePageNewVo> page =  evidenceInventoryService.getEvidencePage(evidenceQueryDto);
		if(ObjectUtil.isNotEmpty(page.getData())){
			page.getData().forEach(u -> reList.add(u.getEvidenceName()));
		}
		//获取线下文书
		List<CaseOfflineDocumentRecord> offlineDocumentRecords = caseOfflineDocumentRecordService.getCaseOfflineDocumentRecord(caseId, CaseStageEnum.INVESTIGATION.getCode());
		if(ObjectUtil.isNotEmpty(offlineDocumentRecords)){
			offlineDocumentRecords.forEach(u -> reList.add(u.getDocumentTittle()));
		}
		//获取调查取证阶段除了“案件调查终结报告”之外，所有生成的文书(包含了询问笔录、现场检查记录表)
		List<CaseDocumentRecordCodeNameVo> records = documentRecordService.getCaseDocumentRecordByStageCode(caseId, CaseStageEnum.INVESTIGATION.getCode(), caseMainInfoVo.getBusiParamId());
		if(ObjectUtil.isNotEmpty(records)){
			records.forEach(u -> reList.add(u.getDocumentCatalogName()));
		}
		FastCaseDocumentDataVo reVo = new FastCaseDocumentDataVo();
		reVo.setEvidenceNames(CollUtil.join(reList, ","));
		return reVo;
	}
}
